import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;


public class client extends JFrame{

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JTextArea intrebareArea = null;
	private JList jList = null;
	private JCheckBox jCheckBox = null;
	private JCheckBox jCheckBox1 = null;
	private JCheckBox jCheckBox2 = null;
	private JScrollPane scrollingArea = null;
	private JScrollPane jScrollPane = null;
	private JButton jButton1 = null;
	private JLabel jLabel = null;
	private JTextPane jTextPane1 = null;
	private JTextPane jTextPane2 = null;
	private JTextPane jTextPane3 = null;
	public void newQuestion(String str) 
	{
		int nr=1;
		String intr="";
		String r1="";
		String r2="";
		String r3="";
		String mes[]=new String[2];
		mes=str.split("#");
		if (mes[0].equals("intr")||mes==null)
		{
			String val[]=new String[5];
			val=mes[1].split("\\&");
			for(int i=0;i<val.length;i++)
			{
				String val2[] ;
				if (val[i].startsWith("intr"))
					val2=val[i].split("=",2);
				else
					val2=val[i].split("=");
				if (val2[0].equals("nr"))
					nr=Integer.parseInt(val2[1]);
				else if (val2[0].equals("intr"))
					intr=val2[1];
				else if (val2[0].equals("r1"))
					r1=val2[1];
				else if (val2[0].equals("r2"))
					r2=val2[1];
				else if (val2[0].equals("r3"))
					r3=val2[1];
			}
			
			principal.curent=nr;
			//jTextPane.setText(intr);
			intrebareArea.setText(intr);
			jTextPane1.setText(r1);
			jTextPane2.setText(r2);
			jTextPane3.setText(r3);
			jCheckBox.setSelected(false);
			jCheckBox1.setSelected(false);
			jCheckBox2.setSelected(false);
			jLabel1.setText("Intrebarea"+nr+": ");
			jLabel.setText("Numarul intrebarilor la care ati raspuns corect: "+principal.numarpuncte());
			jLabel.invalidate();
			validate();
		}
	}
	
	
	public client(String nume) {
		super();
		initialize(nume);
	}

	private void initialize(String nume) {
        this.setSize(new Dimension(600, 593));
        this.setContentPane(getJPanel());
        this.setTitle(nume);
        this.setLocationRelativeTo(null);
	}

	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(14, 330, 350, 19));
			jLabel.setText("Numarul intrebarilor la care ati raspuns corect: 0");
			jLabel.invalidate();
			validate();
			
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(1, 21, 105, 25));
			jLabel1.setText("Intrebare:");
			
			jPanel = new JPanel();
			jPanel.setLayout(null);
			
			jPanel.add(getJButton(), null);
			
			jPanel.add(jLabel1, null);
			
			jPanel.add(getScrollingArea(),null);
			
			jPanel.add(getJCheckBox(), null);			
			jPanel.add(getJCheckBox1(), null);			
			jPanel.add(getJCheckBox2(), null);
			
			jPanel.add(getJScrollPane(), null);
			
			jPanel.add(getJButton1(), null);
			
			jPanel.add(jLabel, null);
			
			jPanel.add(getJTextPane1(), null);
			jPanel.add(getJTextPane2(), null);
			jPanel.add(getJTextPane3(), null);
		}
		return jPanel;
	}

	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(95, 295, 130, 25));
			jButton.setText("Valideaza");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					if(principal.este_in_sir(principal.curent))
						System.out.println("Aceasta intrebare a fost deja validata!");
					else
					{
						String opt1, opt2,opt3;
						opt1=opt2=opt3="0";
						if(jCheckBox.isSelected())
							opt1="1";
						if(jCheckBox1.isSelected())
							opt2="1";
						if(jCheckBox2.isSelected())
							opt3="1";
						String val=new String(opt1+opt2+opt3);
						principal.raspuns(Integer.parseInt(val),principal.curent);
						principal.sir.add(principal.curent);
						jLabel.setText("Numarul intrebarilor la care ati raspuns corect: "+principal.numarpuncte());
					}
				}
			});
		}
		return jButton;
	}

	private JTextArea getIntrebareArea() {
		if(intrebareArea == null) {
			intrebareArea = new JTextArea();
			intrebareArea.setBounds(new Rectangle(100,17,460,81));
			intrebareArea.setEditable(false);
		}
		return intrebareArea;
	}
	
	private JScrollPane getScrollingArea() {
		if (scrollingArea == null) {
			scrollingArea = new JScrollPane();
			scrollingArea.setBounds(new Rectangle(100, 17, 460, 81));
			scrollingArea.setViewportView(getIntrebareArea());
		}
		return scrollingArea;
	}
	
	private JList getJList() {
		if (jList == null) {
			DefaultListModel lm =new DefaultListModel();
			
			for(int i=1;i<=10;i++)
				lm.addElement(i);
			jList = new JList(lm);
			jList.addMouseListener(new java.awt.event.MouseListener() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (principal.este_in_sir(jList.getSelectedIndex()+1))
					{
						System.out.println("Ati raspuns deja!");
					}
					else
					{
						//principal.curent=jList.getSelectedIndex()+1;
						principal.intrebare(jList.getSelectedIndex()+1);
					}	
							
				}
				public void mousePressed(java.awt.event.MouseEvent e) {
				}
				public void mouseReleased(java.awt.event.MouseEvent e) {
				}
				public void mouseEntered(java.awt.event.MouseEvent e) {
				}
				public void mouseExited(java.awt.event.MouseEvent e) {
				}
			});
			}
		return jList;
	}

	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setBounds(new Rectangle(90, 124, 20, 20));
		}
		return jCheckBox;
	}

	private JCheckBox getJCheckBox1() {
		if (jCheckBox1 == null) {
			jCheckBox1 = new JCheckBox();
			jCheckBox1.setBounds(new Rectangle(90, 181, 20, 20));
		}
		return jCheckBox1;
	}
	
	private JCheckBox getJCheckBox2() {
		if (jCheckBox2 == null) {
			jCheckBox2 = new JCheckBox();
			jCheckBox2.setBounds(new Rectangle(90, 239, 20, 20));
			jCheckBox2.setHorizontalTextPosition(SwingConstants.LEFT);
		}
		return jCheckBox2;
	}

	
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(8, 52, 71, 264));
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}


	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(243, 296, 106, 25));
			jButton1.setText("Stop Test");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					principal.numarpunctefinal();
				}
			});
		}
		return jButton1;
	}

	private JTextPane getJTextPane1() {
		if (jTextPane1 == null) {
			jTextPane1 = new JTextPane();
			jTextPane1.setBounds(new Rectangle(120, 108, 220, 50));
			jTextPane1.setEditable(false);
		}
		return jTextPane1;
	}

	private JTextPane getJTextPane2() {
		if (jTextPane2 == null) {
			jTextPane2 = new JTextPane();
			jTextPane2.setBounds(new Rectangle(120, 172, 220, 50));
			jTextPane2.setEditable(false);
		}
		return jTextPane2;
	}

	private JTextPane getJTextPane3() {
		if (jTextPane3 == null) {
			jTextPane3 = new JTextPane();
			jTextPane3.setBounds(new Rectangle(120, 230, 220, 50));
			jTextPane3.setEditable(false);
		}
		return jTextPane3;
	}

} 
